package com.game.business.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.business.service.ITWalletService;
import com.game.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.TLevelMapper;
import com.game.business.domain.TLevel;
import com.game.business.service.ITLevelService;

/**
 * 等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@Service
public class TLevelServiceImpl extends ServiceImpl<TLevelMapper,TLevel>  implements ITLevelService
{
    @Autowired
    private TLevelMapper tLevelMapper;

    /**
     * 查询等级
     *
     * @param id 等级主键
     * @return 等级
     */
    @Override
    public TLevel selectTLevelById(String id)
    {
        return tLevelMapper.selectTLevelById(id);
    }

    /**
     * 查询等级列表
     *
     * @param tLevel 等级
     * @return 等级
     */
    @Override
    public List<TLevel> selectTLevelList(TLevel tLevel)
    {
        return tLevelMapper.selectTLevelList(tLevel);
    }

    /**
     * 新增等级
     *
     * @param tLevel 等级
     * @return 结果
     */
    @Override
    public int insertTLevel(TLevel tLevel)
    {
                tLevel.setCreateTime(DateUtils.getNowDate());
            return tLevelMapper.insertTLevel(tLevel);
    }

    /**
     * 修改等级
     *
     * @param tLevel 等级
     * @return 结果
     */
    @Override
    public int updateTLevel(TLevel tLevel)
    {
                tLevel.setUpdateTime(DateUtils.getNowDate());
        return tLevelMapper.updateTLevel(tLevel);
    }

    /**
     * 批量删除等级
     *
     * @param ids 需要删除的等级主键
     * @return 结果
     */
    @Override
    public int deleteTLevelByIds(String[] ids)
    {
        return tLevelMapper.deleteTLevelByIds(ids);
    }


    @Autowired
    private ITWalletService tWalletService;
    /**
     * 删除等级信息
     *
     * @param id 等级主键
     * @return 结果
     */
    @Override
    public int deleteTLevelById(String id)
    {
        return tLevelMapper.deleteTLevelById(id);
    }
    @Override
    public   TLevel  getCurrLevel(){
        BigDecimal totalXhMoney = tWalletService.getTotalXhMoney().negate();
        List<TLevel> list = this.selectTLevelList(new TLevel());
        // 遍历列表 判断用户等级，以及距离下一等级还差多少金额
        // 添加用户等级信息和升级所需金额
        TLevel  currentLevel = new TLevel();
        int i = 0;
        for (TLevel level : list) {
            if (totalXhMoney.compareTo(level.getMinMoney()) >= 0 &&
                    (level.getMinMoney() == null || totalXhMoney.compareTo(level.getMaxMoney()) < 0)) {
                // 当前等级标记
                level.setIsCurrentLevel(true);
            } else {
                level.setIsCurrentLevel(false);
            }
            if(level.getIsCurrentLevel()){
                TLevel  nextvel =null;
                if(i<list.size()-1){
                    nextvel = list.get(i + 1);
                }
                // 计算距离下一等级还需多少金额
                if(nextvel!=null){
                    level.setAmountToNextLevel(nextvel.getMinMoney().subtract(totalXhMoney));
                }
                currentLevel=level;
                break;
            }
            i++;
        }
        currentLevel.setTotalXhMoney(totalXhMoney);
        return currentLevel;
    }
}
